package fr.irit.transf.refactoring.uml;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;

import fr.irit.etracetool.etrace.ETrace;
import fr.irit.etracetool.etrace.EtracePackage;


public class ModelUtility {

	/**
	 * @param args
	 */
	
	public static Model getModel(String location) {
		ResourceSet resourceSet = new ResourceSetImpl();
		registerPackages(resourceSet);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
		Resource resource = resourceSet.getResource(URI.createFileURI(new File(location).getAbsolutePath()), true);
		//Resource r2=resourceSet.createResource(URI.createFileURI(new File(location).getAbsolutePath()));
		//r2.
		return (Model) resource.getContents().get(0);
	}
	
	
	
	protected  static void registerPackages(ResourceSet resourceSet) {
		Registry packageRegistry = resourceSet.getPackageRegistry();
		packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
//		packageRegistry.put(Ecore2XMLPackage.eNS_URI,Ecore2XMLPackage.eINSTANCE);
		packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
		packageRegistry.put(EtracePackage.eNS_URI, EtracePackage.eINSTANCE);

	}
	public static ETrace getEtrace(String location) {
		ResourceSet resourceSet = new ResourceSetImpl();
		registerPackages(resourceSet);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
		Resource resource = resourceSet.getResource(URI.createFileURI(new File(location).getAbsolutePath()), true);
		return (ETrace) resource.getContents().get(0);
	}

    protected static void save(org.eclipse.uml2.uml.Model m, String loc) {
    	URI uri = URI.createFileURI(new File(loc).getAbsolutePath());
        Resource resource = new ResourceSetImpl().createResource(uri);
        resource.getContents().add(m);
           try {
                resource.save(null);

           } catch (IOException ioe) {
               	System.err.println(ioe.getMessage());
           }
    }

    protected static EObject searchEObjectInCollection(Collection<? extends EObject> collection, EObject eobj) {
    	for (EObject e: collection) {
    		if (EcoreUtil.equals(e, eobj)) return e;
    	}
    	System.err.println("ModelUtility.searchByURI : EObject not found");
    	return null;
    }
    
	protected static void save(EObject m, String loc) {
		URI uri = URI.createFileURI(new File(loc).getAbsolutePath());
	    Resource resource = new ResourceSetImpl().createResource(uri);
	    resource.getContents().add(m);
	       try {
	            resource.save(null);
	
	       } catch (IOException ioe) {
	           	System.err.println(ioe.getMessage());
	       }
	}
	
	public static Resource getResource(String location) {
		ResourceSet resourceSet = new ResourceSetImpl();
		registerPackages(resourceSet);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl());
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("uml", new XMIResourceFactoryImpl());
		Resource resource = resourceSet.getResource(URI.createFileURI(new File(location).getAbsolutePath()), true);
		return resource;
	}
}

